package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chixing.entity.Company;
import com.chixing.mapper.CompanyMapper;
import com.chixing.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-17
 */
@Service
public class CompanyServiceImpl  implements ICompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public Result getById(Integer id) {
        Company company = companyMapper.selectById(id);
        if (company!=null)
            return Result.getSuccess(company);
        return Result.getFail(company);
    }

    @Override
    public Result getAll() {
        List<Company> companies = companyMapper.selectList(null);
        if (companies != null && companies.size()>0)
            return Result.getSuccess(companies);
        return Result.getFail(companies);
    }

    @Override
    public Result getByConditions(String companyName, String companyIndustry, String companyCity) {
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.like("company_name","%"+companyName+"%")
                .eq("company_industry",companyIndustry)
                .eq("company_city",companyCity);
        List<Company> companies = companyMapper.selectList(wrapper);
        if (companies!=null&&companies.size()>0)
            return Result.getSuccess(companies);
        return Result.getFail(companies);
    }

    @Override
    public Result save(Company company) {
        company.setCompanyCreatedAt(LocalDateTime.now());
        int rows = companyMapper.insert(company);
        if (rows>0)
            return Result.saveSuccess(company);
        return Result.saveFail(company);
    }

    @Override
    public Result update(Company company) {
        company.setCompanyUpdatedAt(LocalDateTime.now());
        int rows = companyMapper.updateById(company);
        if (rows>0)
            return Result.updateSuccess(company);
        return Result.updateFail(company);
    }

    @Override
    public Result delete(Integer companyId) {
        int rows = companyMapper.deleteById(companyId);
        if (rows>0)
            return Result.DeleteSuccess(companyId);
        return Result.DeleteFail(companyId);
    }

    @Override
    public Result getPage(Integer pageNumber) {
        Page<Company> page = new Page<>(pageNumber, 3);
        page = companyMapper.selectPage(page, null);
        if (page.getTotal()>0)
            return Result.getSuccess(page);
        return Result.getFail(null);
    }
}
